package com.example.demo.lambda;

import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @Author: xiatian
 * @CreateTime: 2021/10/21 2:56 下午
 * @Description:
 */
public class TestLimit {


    @Test
    public void testLimit() {
        List<Person> persons = new ArrayList();
        for (int i = 1;i <= 5; i++) {
            Person person = new Person(i, "name" + i);
            persons.add(person);
        }

        List<Person> personList2 = persons.stream().sorted((p1, p2) ->
                p1.getName().compareTo(p2.getName())).limit(2).collect(Collectors.toList());
        System.out.println(personList2.stream().map(x->x.getName()).collect(Collectors.toList()));
    }


    private class Person {
        private Integer no;
        private String name;

        public Person(Integer no, String name) {
            this.no = no;
            this.name = name;
        }

        public Integer getNo() {
            return no;
        }

        public void setNo(Integer no) {
            this.no = no;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
